package com.xbongbong.crm.service.condition;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.abstracts.AbstractConditionService;
import com.xbongbong.parent.pojo.list.ConditionListPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.dictionary.CustomerTypeEnum;
import com.xbongbong.saas.model.CustomerFocusModel;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/06/15 16:36
 */
@Service("customerConditionServiceImpl")
public class CustomerConditionServiceImpl extends AbstractConditionService {

    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private CustomerFocusModel customerFocusModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), XbbRefTypeEnum.CUSTOMER.getCode(), XbbRefTypeEnum.CUSTOMER_DEAL.getCode(), XbbRefTypeEnum.CUSTOMER_FOCUS.getCode(), XbbRefTypeEnum.CUSTOMER_FOLLOW.getCode(), XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode());
    }

    @Override
    public List<ConditionsEntityExt> packageConditionBySubBusinessType(ConditionListPojo conditionListPojo) {
        List<ConditionsEntityExt> conditionList = new ArrayList<>();
        Integer subBusinessType = conditionListPojo.getSubBusinessType();
        if (Objects.equals(XbbRefTypeEnum.CUSTOMER_RECYCLE.getCode(), subBusinessType)) {
            // 回收站不需要调用这个方法
            return conditionList;
        }
        String corpid = conditionListPojo.getCorpid();
        String userId = conditionListPojo.getUserVO().getUserId();
        Integer statusFilter = conditionListPojo.getStatusFilter();
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(subBusinessType);

        Integer isPublic = 0;
        switch (xbbRefTypeEnum) {
            case CUSTOMER_DEAL:
                //conditionList筛选条件
                //客户类型为成交客户
                ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
                conditionsEntityExt.setAttr(CustomerManagementEnum.TYPE.getAttr());
                conditionsEntityExt.setFieldType(CustomerManagementEnum.TYPE.getFieldType());
                conditionsEntityExt.setName(CustomerManagementEnum.TYPE.getAttrName());
                conditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
                conditionsEntityExt.setValue(Collections.singletonList(CustomerTypeEnum.TRANSACTION_CUSTOMER.getCode()));
                conditionList.add(conditionsEntityExt);
                break;
            case CUSTOMER_FOCUS:
                boolean isShare;
                try {
                    isShare = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.SHARE_FOCUS_CUSTOEMR.getAlias(), BasicConstant.IS_USE.toString());
                } catch (XbbException e) {
                    isShare = Objects.equals(CompanyConfigEnum.SHARE_FOCUS_CUSTOEMR.getValue(), BasicConstant.IS_USE.toString());
                }
                List<Long> dataIdList;
                if (isShare) {
                    dataIdList = customerFocusModel.getShareCustomerIdList(corpid);
                } else {
                    dataIdList = customerFocusModel.getNoShareCustomerIdList(userId, corpid);
                }
                dataIdList.add(-1L);
                conditionsEntityExt = new ConditionsEntityExt();
                conditionsEntityExt.setAttr(FieldTypeEnum.DATAID.getAlias());
                conditionsEntityExt.setFieldType(FieldTypeEnum.DATAID.getType());
                conditionsEntityExt.setName(FieldTypeEnum.DATAID.getDescription());
                conditionsEntityExt.setSymbol(ConditionEnum.IN.getSymbol());
                conditionsEntityExt.setValue(new ArrayList<>(dataIdList));
                conditionList.add(conditionsEntityExt);
                break;
            case CUSTOMER_FOLLOW:
                //客户类型不为成交客户、忠诚客户、无效客户
                conditionsEntityExt = new ConditionsEntityExt();
                conditionsEntityExt.setAttr(CustomerManagementEnum.TYPE.getAttr());
                conditionsEntityExt.setFieldType(CustomerManagementEnum.TYPE.getFieldType());
                conditionsEntityExt.setName(CustomerManagementEnum.TYPE.getAttrName());
                conditionsEntityExt.setSymbol(ConditionEnum.NOIN.getSymbol());
                conditionsEntityExt.setValue(Arrays.asList(CustomerTypeEnum.TRANSACTION_CUSTOMER.getCode(), CustomerTypeEnum.LOYAL_CUSTOMERS.getCode(), CustomerTypeEnum.INVALID_CUSTOMER.getCode()));
                conditionList.add(conditionsEntityExt);
                break;
            case CUSTOMER_PUBLIC:
                isPublic = 1;
                break;
            default:
                break;
        }
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(CustomerManagementEnum.IS_PUBLIC.getAttr());
        conditionsEntityExt.setFieldType(CustomerManagementEnum.IS_PUBLIC.getFieldType());
        conditionsEntityExt.setName(CustomerManagementEnum.IS_PUBLIC.getAttrName());
        conditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        conditionsEntityExt.setValue(Collections.singletonList(isPublic));
        conditionList.add(conditionsEntityExt);
        if (!Objects.equals(subBusinessType, XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode()) && !(Objects.isNull(statusFilter) || statusFilter == 0)){
            ConditionsEntityExt statusFilterEntityExt = new ConditionsEntityExt();
            statusFilterEntityExt.setAttr(CustomerManagementEnum.IS_ARCHIVED.getAttr());
            statusFilterEntityExt.setFieldType(CustomerManagementEnum.IS_ARCHIVED.getFieldType());
            statusFilterEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
            statusFilterEntityExt.setValue(Collections.singletonList(statusFilter));
            conditionList.add(statusFilterEntityExt);
        }
        return conditionList;
    }
}
